import {
  getMenuList
} from '../../api/index.js'
Page({
  data: {
    scrollTop: undefined,
    loadingShow: true,
    loadingType: 'loading'
  },
  $data: {
    query: {
      currentPage: 1,
      pageNumber: 10,
    },
    loadingStatus: {
      loading: 'loading',
      end: 'end'
    }
  },
  onLoad(options) {
    this.getList()
  },
  onPullDownRefresh() {
    this.$data.query.currentPage = 1
    this.getList(true)
  },
  onReachBottom() {
    if (this.data.loadingType === "loading") {
      this.$data.query.currentPage++
      this.getList()
    }
  },
  onShareAppMessage() {},

  onPageScroll(res) {
    this.setData({
      scrollTop: res.scrollTop
    })
  },

  getList(isInit = false) {
    const {
      query,
      loadingStatus
    } = this.$data

    getMenuList(query).then((res) => {
      const list = res.data
      const loadingType = list.length < query.pageNumber ? loadingStatus.end : loadingStatus.loading
      if (list.length === 0) {
        this.setData({
          loadingType
        })
        wx.stopPullDownRefresh()
        wx.lin.flushSticky()
        return
      }
      wx.lin.renderWaterFlow(list, isInit, () => {
        this.setData({
          loadingType
        })
        wx.stopPullDownRefresh()
        wx.lin.flushSticky()
      })
    })
  },

  onMenuDetail(e) {
    const menuItem = e.detail.item;
    wx.navigateTo({
      url: `/pages/MenuDetail/index?id=${menuItem._id}`,
    })
  }
})